package Compation;

import Utils.ListNode;
import Utils.Pair;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UndergroundSystem {
    public static void main(String[] args) {
        UndergroundSystem undergroundSystem = new UndergroundSystem();
        undergroundSystem.checkIn(596854,"EQH524YN",13);
        undergroundSystem.checkIn(29725,"Y1A2ROGU",17);
        undergroundSystem.checkOut(596854,"8AYN1B7O",115);
        undergroundSystem.checkIn(579716,"WOO3SB09",198);
        undergroundSystem.getAverageTime("EQH524YN","8AYN1B7O");
        undergroundSystem.checkIn(126763,"V616MTS5",263);
        undergroundSystem.checkOut(579716,"Z0LX9XF2",348);
        undergroundSystem.checkOut(29725,"ZX5B4CTZ",431);
        System.out.println(undergroundSystem.getAverageTime("EQH524YN","8AYN1B7O"));
        undergroundSystem.checkOut(126763,"M89OZ6WW",490);
        undergroundSystem.checkIn(691237,"AY6I79N1",498);
        undergroundSystem.checkIn(597688,"M605W0WA",567);

    }

    /*
    People的key为ID,存储了进入与出站的时间
     */
    private static Map<Integer, People> people = new HashMap<>();

    /*
    存储站名
     */
    private static List<String> stations = new ArrayList<>();
    /*
    存储从station[i]到station[j]的时间总和
     */
    private static int[][] timeMatrix = new int[10][10];

    /*
    存储从station[i]到station[j]的人数总和
     */
    private static int[][] peopleMatrix = new int[10][10];

    public UndergroundSystem() {

    }

    public void checkIn(int id, String stationName, int t) {
        people.put(id, new People(stationName, "", t, -1));
        if (stations == null || !stations.contains(stationName)) {
            stations.add(stationName);
        }
    }

    public void checkOut(int id, String stationName, int t) {
        people.put(id, new People(people.get(id).inStation, stationName, people.get(id).inTime, t));
        if (stations == null || !stations.contains(stationName)) {
            stations.add(stationName);
        }
        int inTime = people.get(id).inTime;
        int outTime = people.get(id).outTime;
        int inStationIndex = stations.indexOf(people.get(id).inStation);
        int outStationIndex = stations.indexOf(people.get(id).outStation);
        timeMatrix[inStationIndex][outStationIndex] += outTime - inTime;
        peopleMatrix[inStationIndex][outStationIndex] += 1;
    }

    public double getAverageTime(String startStation, String endStation) {
        int inStationIndex = stations.indexOf(startStation);
        int outStationIndex = stations.indexOf(endStation);
        double time = timeMatrix[inStationIndex][outStationIndex] / peopleMatrix[inStationIndex][outStationIndex];
        return time;
    }

    class People {
        String inStation;
        String outStation;
        int inTime;
        int outTime;

        public People(String inStation, String outStation, int inTime, int outTime) {
            this.inStation = inStation;
            this.outStation = outStation;
            this.inTime = inTime;
            this.outTime = outTime;
        }
    }

    /**
     * 方法二：创建两个map
     * 第一个map: Map<Integer, Pair<String, Integer> map ,其中Key为每个行人的ID，Pair为每个行人对应的入站station和时间
     * 第二个map: Map<String, Pair<Integer, Float> time, 其中Key为入站 + 出站的名字，Pair为 对应的总时间 和 总次数
     */
    private Map<Integer, Pair<String, Integer>> map = new HashMap<>();

    private Map<String, Pair<Integer, Float>> time = new HashMap<>();

    public void checkInI(int id, String stationName, int t) {
        map.put(id, new Pair<>(stationName, t));
    }

    public void checkOutI(int id, String stationName, int t) {
        Pair<String, Integer> StationAndTime = map.get(id);
        String fullPathName = StationAndTime.getKey() + stationName;
        Pair<Integer, Float> pathTime = time.getOrDefault(fullPathName, new Pair<>(0, 0.0f));
        int newTime = pathTime.getKey() - StationAndTime.getValue() + t;
        time.put(fullPathName, new Pair<>(newTime, pathTime.getValue() + 1.0f));
        map.remove(id);
    }

    public double getAverageTimeI(String startStation, String endStation) {
        String fullPath = startStation + endStation;
        Pair<Integer, Float> res = time.get(fullPath);
        return res.getKey() / res.getValue();
    }
}

